package com.mdinfo.gw.door.mapper

import com.baomidou.mybatisplus.mapper.BaseMapper
import com.mdinfo.gw.door.bean.DoorEvent
import com.mdinfo.gw.door.vo.DoorEventListQueryVo
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import java.util.*

/**
 * Created by Administrator on 2018/1/8.
 */
interface DoorEventMapper : BaseMapper<DoorEvent>{


    /**列表分页查询数据*/
    @Select("""<script>
        select e.*,
        d.`name` doorName,a.`name` accessName,
        c.devId cardDevId,
        sd.shortName deptName,
        emp.`name` empName,
        emp.id empId
        from door_event e
        join door d on (e.doorId = d.id)
        join door_access a on (e.doorAccessId = a.id)
        left join door_card c on (e.cardId = c.id)
        join sys_dept sd on (e.deptId = sd.id)
        left join sys_emp emp on (c.empId = emp.id)
        where 1=1
        <if test="doorId!=null">
            and e.doorId = #{doorId}
        </if>
        <if test="doorAccessId!=null">
            and e.doorAccessId = #{doorAccessId}
        </if>
        <if test="cardId!=null">
            and e.cardId = #{cardId}
        </if>
        <if test="deptId!=null">
            and e.deptId = #{deptId}
        </if>
        <if test="key!=null">
            and (
                d.`name` like CONCAT('%',#{key},'%')
	            OR
	            a.`name` like CONCAT('%',#{key},'%')
                or
	            c.devId like CONCAT('%',#{key},'%')
                or
                emp.name like CONCAT('%',#{key},'%')
            )
        </if>
        <if test="doorEvent!=null">
            and e.doorEvent = #{doorEvent}
        </if>
        <if test="startTime!=null">
             <![CDATA[ and e.createTime >= #{startTime,jdbcType=DATE} ]]>
        </if>
        <if test="endTime!=null">
             <![CDATA[ and e.createTime < #{endTime,jdbcType=DATE} ]]>
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and e.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
        order by e.createTime desc
        limit #{start},#{num}
    </script>""")
    fun listQuery(@Param("start")start:Int,
                  @Param("num")num:Int,
                  @Param("doorId")doorId:Int?,
                  @Param("doorAccessId")doorAccessId:Int?,
                  @Param("cardId")cardId:Int?,
                  @Param("deptId")deptId:Int?,
                  @Param("key")key:String?,
                  @Param("doorEvent")doorEvent:Int?,
                  @Param("startTime")startTime: Date?,
                  @Param("endTime")endTime: Date?,
                  @Param("deptIdList")deptIdList:List<Int>)
            :List<DoorEventListQueryVo>

    /**列表查询总数*/
    @Select("""<script>
        select count(1)
        from door_event e
        join door d on (e.doorId = d.id)
        join door_access a on (e.doorAccessId = a.id)
        left join door_card c on (e.cardId = c.id)
        join sys_dept sd on (e.deptId = sd.id)
        left join sys_emp emp on (c.empId = emp.id)
        where 1=1
        <if test="doorId!=null">
            and e.doorId = #{doorId}
        </if>
        <if test="doorAccessId!=null">
            and e.doorAccessId = #{doorAccessId}
        </if>
        <if test="cardId!=null">
            and e.cardId = #{cardId}
        </if>
        <if test="deptId!=null">
            and e.deptId = #{deptId}
        </if>
        <if test="key!=null">
            and (
                d.`name` like CONCAT('%',#{key},'%')
	            OR
	            a.`name` like CONCAT('%',#{key},'%')
                or
	            c.devId like CONCAT('%',#{key},'%')
                or
                emp.name like CONCAT('%',#{key},'%')
            )
        </if>
        <if test="doorEvent!=null">
            and e.doorEvent = #{doorEvent}
        </if>
        <if test="startTime!=null">
             <![CDATA[ and e.createTime >= #{startTime,jdbcType=DATE} ]]>
        </if>
        <if test="endTime!=null">
             <![CDATA[ and e.createTime < #{endTime,jdbcType=DATE} ]]>
        </if>
        <if test="deptIdList!=null and deptIdList.size>0">
            and e.deptId in
            <foreach collection="deptIdList" item="dept_id" open="(" close=")" separator=",">
                #{dept_id}
            </foreach>
        </if>
    </script>""")
    fun countListQuery(@Param("doorId")doorId:Int?,
                       @Param("doorAccessId")doorAccessId:Int?,
                       @Param("cardId")cardId:Int?,
                       @Param("deptId")deptId:Int?,
                       @Param("key")key:String?,
                       @Param("doorEvent")doorEvent:Int?,
                       @Param("startTime")startTime: Date?,
                       @Param("endTime")endTime: Date?,
                       @Param("deptIdList")deptIdList:List<Int>)
            :Int
}